let payOrderDao = require('../dao/payOrderDao');
let mysqlPool = require('../dao/MySQLPool');
let userDao = require('../dao/userDao');
let payConst = require('../const/payConst');

const { BusiError, SysError } = require('../exception/exception');

class PayOrderCoinsService {
    constructor(){
        this.serviceName = "C1";
        this.remark = "金币订单处理器";
    }

    async process(order){
        await mysqlPool.doWithTx(async function (conn) {
            let res2 = await payOrderDao.updateByOutTradeNoWithTx(conn, order);
            if (res2 < 1) {
                console.logw("更新订单失败:", order);
                throw new BusiError(500, "更新订单失败");
            }

            if (order.status === payConst.payOrderStatus.orderSuccess) {
                let res1 = await userDao.incCoinWithTx(conn, order.uid, parseFloat(order.amount / 100.00).toFixed(2));
                if (res1 < 1) {
                    console.logw("inc user coin err: res1 <= 0");
                    throw new BusiError(500, "增加余额失败");
                }
            }
        });
    }
}


let payOrderCoinsService = new PayOrderCoinsService();
module.exports = payOrderCoinsService;